package cn.xiaobai.enums;

import cn.xiaobai.domain.Regedit;
import cn.xiaobai.service.RegeditCommandService;
import cn.xioabai.common.utility.NumberUtils;
import cn.xioabai.common.utility.ScannerUtil;
import org.apache.commons.lang3.StringUtils;

import java.util.Scanner;

/**
 * 简单注册表枚举
 *
 * @Author yangdaji
 * @Date 2023/4/27 0027
 * @PackageName cn.xiaobai.enums
 * @ClassName SimpleRegeditEnum
 */
public enum SimpleRegeditEnum {

    /**
     * 返回到主功能列表
     */
    ZERO(0, "返回上一层") {
        @Override
        public void execute() {
        }
    },
    /**
     * 添加右键功能
     */
    ONE(1, "添加功能到右键") {
        @Override
        public void execute() {

            String message = "无法识别输入的序号!";
            // 类型序号
            String typeNumber = "";
            // 功能名称
            String kName = "";
            // 执行的操作
            String exec = "";
            boolean succe = false;
            setLocal();
            Regedit regedit = getLocal();
            while (!succe) {
                promptMessage("添加");
                System.out.print("输入添加类型序号(为空则返回开始):");
                typeNumber = sc.nextLine();
                System.out.print("输入右键功能名称(为空则为default):");
                kName = sc.nextLine();
                System.out.print("输入该功能需要执行操作(如果脚本加上cmd /c前置即可):");
                exec = sc.nextLine();

                // 如果为空则设置为默认
                if (StringUtils.isNoneBlank(kName)) {
                    kName = kName + "\\command";
                } else {
                    kName = regedit.getKName();
                }
                succe = setAttribute(typeNumber, kName, exec, message);
            }
            // 向注册表中添加
            regeditCommandService.addRegedit(regedit);
        }
    },
    /**
     * 删除右键功能
     */
    TWO(2, "删除右键的功能") {
        @Override
        public void execute() {
            String message = "无法识别输入的序号!";
            // 类型序号
            String typeNumber = "";
            // 功能名称
            String kName = "";
            boolean succe = false;
            setLocal();
            while (!succe) {
                promptMessage("删除");
                System.out.print("输入类型序号(为空则返回开始):");
                typeNumber = sc.nextLine();
                System.out.print("输入删除的功能名称:");
                kName = sc.nextLine();
                succe = setAttribute(typeNumber, kName, "", message);
            }
            // 向注册表中删除
            regeditCommandService.removeRegedit(getLocal());
        }
    },
    /**
     * 查询右键功能
     */
    THREE(3, "查询右键的功能") {
        @Override
        public void execute() {
            String message = "无法识别输入的序号!";
            // 类型序号
            String typeNumber = "";
            // 功能名称
            String kName = "";
            boolean succe = false;
            setLocal();
            while (!succe) {
                promptMessage("查询");
                System.out.print("输入类型查询序号(为空则返回开始):");
                typeNumber = sc.nextLine();
                System.out.print("输入查询的功能名称(为空则查询所有):");
                kName = sc.nextLine();
                succe = setAttribute(typeNumber, kName, "", message);
            }
            // 向注册表中查询
            regeditCommandService.queryRegedit(getLocal());
        }
    };


    private Integer number;
    private String name;


    SimpleRegeditEnum(Integer number, String name) {
        this.number = number;
        this.name = name;
    }

    protected static void promptMessage(String type) {
        System.out.println(String.format("1: %s普通右键功能", type));
        System.out.println(String.format("2: %s桌面右键功能", type));
    }

    protected static Boolean setAttribute(String typeNumber, String kName, String exec, String message) {
        // 输入为空, 则返回到开始
        Regedit regedit = getLocal();
        boolean succe = false;
        if (StringUtils.isBlank(typeNumber) || !NumberUtils.isNumber(typeNumber)) {
            System.out.println(message);
        } else {
            succe = true;
            if ("2".equals(typeNumber.trim())) {
                regedit.setIsDeskTopLevel(true);
            }
        }
        // 如果为空则设置为默认
        if (StringUtils.isBlank(kName)) {
            regedit.setKName("default");
        } else {
            regedit.setKName(kName);
        }
        regedit.setKValue(exec);
        threadLocal.set(regedit);
        return succe;
    }

    /**
     * 对应的执行方法
     */
    public abstract void execute();

    private static RegeditCommandService regeditCommandService;

    private static ThreadLocal<Regedit> threadLocal = new ThreadLocal<>();

    private static Scanner sc = ScannerUtil.getScanner();

    public static SimpleRegeditEnum getItem(Integer number) {
        for (SimpleRegeditEnum item : values()) {
            if (item.getNumber().equals(number)) {
                return item;
            }
        }
        return null;
    }

    private static void setLocal() {
        threadLocal.set(new Regedit());
    }

    private static Regedit getLocal() {
        return threadLocal.get();
    }

    public static void setRegeditCommandService(RegeditCommandService regeditCommandService) {
        SimpleRegeditEnum.regeditCommandService = regeditCommandService;
    }


    public Integer getNumber() {
        return number;
    }

    public String getName() {
        return name;
    }

    public static Scanner getSc() {
        return sc;
    }
}
